package com.example.vue_shop.components.shopparams.controller;

import com.example.vue_shop.components.shopparams.entity.ShopParams;
import com.example.vue_shop.components.shopparams.service.ShopParamsService;
import com.example.vue_shop.utils.ResultN;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 分类参数（商品参数）.
 * @author lipengwei
 */
@Api(tags = "分类参数（商品参数）")
@RestController
@RequestMapping("/shopParams")
public class ShopParamsController {

    @Resource(name = "shopParamsServiceImpl")
    private ShopParamsService shopParamsService;

    /**
     * 获取分类参数列表(获取商品参数列表).
     * @param id 商品分类的主键Id
     * @param type 参数类型 dynamic：动态参数，static，静态属性
     * @return String
     */
    @ApiOperation(value = "获取分类参数列表(获取商品参数列表)", notes = "获取分类参数列表(获取商品参数列表)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类的主键Id",
                    required = true, example = "1", dataType = "int"),
            @ApiImplicitParam(name = "type", value = "参数类型 dynamic：动态参数，static，静态属性",
                    required = true, example = "dynamic", dataType = "String")
    })
    @GetMapping("/getShopParamsList")
    public String getShopParamsList(int id, String type) {
        try{
            Map<String, Object> shopParamsList = shopParamsService.getShopParamsList(id, type);
            return ResultN.success(shopParamsList, "获取参数列表成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("获取参数列表失败！");
        }
    }

    @ApiOperation(value = "新增分类参数（商品参数）", notes = "新增分类参数（商品参数）。")
    @PostMapping("/addShopParams")
    public String addShopParams(@RequestBody ShopParams shopParams) {
        try {
            shopParamsService.addShopParams(shopParams);
            return ResultN.success("新增分类参数（商品参数）成功！");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("新增分类参数（商品参数）失败！");
        }
    }

    /**
     * 删除分类参数（商品参数）.
     * @param id 分类参数（商品参数）的 Id
     * @return String
     */
    @ApiOperation(value = "删除分类参数（商品参数）", notes = "删除分类参数（商品参数）。")
    @ApiImplicitParam(name = "id", value = "分类参数（商品参数）主键Id",
            required = true, dataType = "int")
    @DeleteMapping("/delShopParams")
    public String delShopParams(int id) {
        try {
            shopParamsService.delShopParams(id);
            return ResultN.success("删除分类参数（商品参数成功）");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("删除分类参数（商品参数失败）");
        }
    }

    /**
     * 更新分类参数（商品参数）
     * @param shopParams 分类参数（商品参数）实体对象
     * @return String
     */
    @ApiOperation(value = "更新分类参数（商品参数）", notes = "更新分类参数（商品参数。")
    @PutMapping("/updateShopParams")
    public String updateShopParams(ShopParams shopParams){
        try {
            shopParamsService.updateShopParams(shopParams);
            return ResultN.success("更新分类参数（商品参数成功）");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("更新分类参数（商品参数失败）");
        }
    }


}
